package com.zwf.controller.admin;

import com.zwf.constant.StatusConstant;
import com.zwf.dto.DishDTO;
import com.zwf.dto.DishPageQueryDTO;
import com.zwf.entity.Dish;
import com.zwf.result.PageResult;
import com.zwf.result.Result;
import com.zwf.service.DishService;
import com.zwf.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * @Author zwf
 * @date 2024/2/2 17:16
 */

/**
 * 菜品管理
 */
@RestController("adminDishController")
@RequestMapping("/admin/dish")
@Slf4j
@Api(tags = "菜品相关接口")
public class DishController {

    @Autowired
    DishService dishService;

    @Autowired
    RedisTemplate redisTemplate;

    @PostMapping
    @ApiOperation("新增菜品")
    public Result add(@RequestBody DishDTO dishDTO) {
        log.info("新增菜品：{}", dishDTO);
        dishService.add(dishDTO);

        //清除redis中该菜品所属分类的缓存
        String key="dish_"+dishDTO.getCategoryId();
        cleanCache(key);

        return Result.success();
    }

    @GetMapping("page")
    @ApiOperation("菜品分页查询")
    public Result<PageResult> pageQuery(DishPageQueryDTO dishPageQueryDTO) {
        log.info("菜品分页查询：{}", dishPageQueryDTO);
        PageResult pageResult = dishService.pageQuery(dishPageQueryDTO);
        return Result.success(pageResult);
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id查询菜品及其关联的口味")
    public Result<DishVO> queryById(@PathVariable Long id) {
        log.info("根据id查询菜品及其关联的口味：{}", id);
        DishVO dishVO = dishService.queryByIdWithFlavor(id);
        return Result.success(dishVO);
    }

    @GetMapping("/list")
    @ApiOperation("根据分类id查询菜品")
    public Result<List<Dish>> queryByCategoryId(Long categoryId) {
        log.info("根据分类id查询菜品：{}", categoryId);
        List<Dish> list = dishService.queryByCategoryId(categoryId);
        return Result.success(list);
    }

    @PostMapping("/status/{status}")
    @ApiOperation("菜品起售、停售")
    public Result enAndDis(@PathVariable Integer status,Long id) {
        log.info("菜品：{}",id);
        if (status == StatusConstant.ENABLE) {
            log.info("起售");
        } else if (status == StatusConstant.DISABLE) {
            log.info("停售");
        }
        dishService.enAndDis(status,id);

        //清除所有redis中的菜品缓存数据，以dish_*开头
        cleanCache("dish_*");

        return Result.success();
    }

    @PutMapping
    @ApiOperation("修改菜品")
    public Result modify(@RequestBody DishDTO dishDTO){
        log.info("修改菜品：{}",dishDTO);
        dishService.modify(dishDTO);

        //清除所有redis中的菜品缓存数据，以dish_*开头
        cleanCache("dish_*");

        return Result.success();
    }

    @DeleteMapping
    @ApiOperation("批量删除菜品")
    public Result delete(@RequestParam List<Long> ids){
        log.info("批量删除菜品");
        dishService.delete(ids);

        //清除所有redis中的菜品缓存数据，以dish_*开头
        cleanCache("dish_*");

        return Result.success();
    }

    private void cleanCache(String pattern){
        Set keys = redisTemplate.keys(pattern);  //匹配key为pattern的所有key-value
        redisTemplate.delete(keys);
    }
}